package com.zzz.schedule.controller;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.Method;

/*
 * @author zzr
 * @date: 2025/07/14  21:28
 * @description:
 */
public class BaseController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 判断此次请求的目标 ==》 增删改查
        String requestURI = req.getRequestURI(); //  URI可能为:/schedule/add
        String[] split = requestURI.split("/");
        String methodName = split[split.length - 1];

        try {
            // 使用反射的方法，通过方法名获取下面的方法
            Class aClass = this.getClass();
            // 获取方法
            Method declaredMethod = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            // 暴力破解方法的访问修饰符的限定
            declaredMethod.setAccessible(true);
            // 执行方法
            declaredMethod.invoke(this, req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
